#include <QFileDialog>
#include <QMessageBox>
#include "settingsdialog.h"
#include "ui_settingsdialog.h"
#include "databasefacade.h"

SettingsDialog::SettingsDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SettingsDialog)
{
    ui->setupUi(this);

    sd = new CSSettingsInformation;

    ui->txtDatabaseAddress->setText(sd->GetDataaseAddress());
    ui->txtDatabaseName->setText(sd->GetDatabaseName());
    ui->txtDatabasePassword->setText(sd->GetDatabasePassword());
    ui->txtDatabasePort->setText(sd->GetDatabasePort());
    ui->txtDatabaseUserName->setText(sd->GetDatabaseUser());
}

SettingsDialog::~SettingsDialog()
{
    delete sd;
    delete ui;
}

void SettingsDialog::on_buttonBox_accepted()
{
    df = new DataBaseFacade;
    ComResponse *c = new ComResponse;

    sd->WriteDatabaseAddress(ui->txtDatabaseAddress->text());
    sd->WriteDatabaseName(ui->txtDatabaseName->text());
    sd->WriteDatabasePassword(ui->txtDatabasePassword->text());
    sd->WriteDatabasePort(ui->txtDatabasePort->text());
    sd->WriteDatabaseUser(ui->txtDatabaseUserName->text());

    if (df->TestConnection(c))
    {
        QMessageBox::warning(this, "Database Location", "Database Connection OK", QMessageBox::Ok);
    }
    else
    {
        QMessageBox::information(this, "Database Location", c->ErrorMessage, QMessageBox::Ok);
    }

    delete c;
    delete df;

    emit this->DatabaseSettingsUpdated();
}

void SettingsDialog::on_buttonBox_rejected()
{
    emit this->DatabaseSettingsAborted();
}

